// pages/settings/addcommodity/addcommodity.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    category: ["冷冻", "常温"],
    index: 0,
    isnull_name: false,
    isnull_cost: false,
    isnull_unit: false,
    barcode:''
  },


  showTopTips: function() {
    var that = this;
    that.setData({
      showTopTips: true
    });
    setTimeout(function() {
      that.setData({
        showTopTips: false
      });
    }, 1500);
  },

  formSubmit(e) {
    var that = this
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    if (this.is_null(e)) {
      wx.getStorage({
        key: 'openid',
        success: function(res) {
          wx.request({
            url: 'http://192.168.3.2/cloudwarehouse/settings/commodity/addcommodity/addcommodity.php',
            data: {
              name: e.detail.value.name,
              cost: e.detail.value.cost,
              unit: e.detail.value.unit,
              category: e.detail.value.category,
              seller: e.detail.value.seller,
              barcode:e.detail.value.barcode,
              openid: res.data
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success(res) {
              if (!res.data) {
                wx.showToast({
                  title: '商品名重复',
                  image: '/icons/stop.png'
                })
                return;
              } else{
                wx.showToast({
                  title: '添加成功',
                })
              setTimeout(function () {
                wx.redirectTo({
                  url: '../commoditymanagement/commoditymanagement',
                })
              }, 1000)}
            }
          })
        },
      })
    } 
  },

  is_null: function(e){
    var that = this
    if (e.detail.value.cost != '' && e.detail.value.name != '' && e.detail.value.unit != '')
      return 1;
    else if (e.detail.value.name == '') {
      that.setData({
        isnull_name: true
      })
    } else (that.setData({
      isnull_name: false
    }))

    if (e.detail.value.cost == '') {
      that.setData({
        isnull_cost: true
      })
    } else (that.setData({
      isnull_cost: false
    }))

    if (e.detail.value.unit == '') {
      that.setData({
        isnull_unit: true
      })
    } else (that.setData({
      isnull_unit: false
    }))
    this.showTopTips()
    return 0;

  },



  bindChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail)
    this.setData({
      index: e.detail.value
    })
  },

  scan: function() {
    var that = this
    wx.scanCode({
      scanType: ['barCode'],
      success(res) {
        console.log(res.result)
        that.setData({
          barcode : res.result
        })
      }
    })
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})